
export class FObject {


    /**
     * 数据是否已经被调用update方法更新过了
     *
     * @type {boolean}
     * @memberof FObject
     */
    public m_update: boolean = false;

    public static get NAME(): string {
        return "FObject";
    }

    public update(data: object): this {
        this.analysis(data);
        this.m_update = true;
        return this
    }

    protected analysis(data: object, ...customPropertys): void {
        ObjectUtility.Analysis(this, this, data, this.customSetProperty, this.unOwnSetProperty, customPropertys);
    }

    /**
    * 当前对象不存在正在解析的属性值
    */
    protected unOwnSetProperty(thisObj: object, data: object, property: string): void {
    }
    /**
     * 设置自定义的属性
     */
    protected customSetProperty(thisObj: object, data: object, property: string): void {
        if (this[property] != null && this[property] instanceof FObject)
            this[property].update(data[property]);
        else this.unOwnSetProperty(thisObj, data, property);
    }
    public clone(): object {
        return null;
    }
}


export class ObjectUtility {
    public ObjectUtility() { }

    // public static GetObjectClassName(o: Object): string {
    //     let str: string = Utils.getQualifiedClassName(o)
    //     return str.substring(str.indexOf("::"))
    // }

    public static SetProperty(target: object, source: object): void {
        for (let property in source) {
            target[property] = source[property]
        }
    }

    public static Analysis(thisObj: any, target: object, source: object, customSetProperty: Function, unOwnProperty: Function, customPropertys: any[]): void {
        if (source == null) { return }
        for (let property in source) {
            if (target.hasOwnProperty(property)) {
                if (customPropertys.length == 0 || customPropertys.indexOf(property) == -1) {
                    if (target[property] != source[property])
                        target[property] = source[property];
                }
                else {
                    if (customSetProperty != null)
                        customSetProperty.call(thisObj, target, source, property);
                }
            }
            else {
                if (unOwnProperty != null)
                    unOwnProperty.call(thisObj, target, source, property)
            }
        }
    }
    public static CloneObject(source: Object): any {
    }

    public static ToArray(v: any): any[] {
        var arr = [];
        for (var i: number = 0; i < v.length; i++)
            arr[i] = v[i];
        return arr;
    }

    public static TransformObjectVector(data: any, clz: any): any[] {
        if (data && data.hasOwnProperty("length")) {
            var arr: any[] = [];
            for (var i: number = 0; i < data.length; i++) {
                if (clz == String || clz == Number || clz == Boolean) arr.push(data[i]);
                else {
                    var v: any = new clz();
                    if (v instanceof FObject) v.update(data[i]);
                    else this.Analysis(this, v, data[i], this.CustomSetProperty, this.CustomSetProperty, []);
                    arr.push(v);
                }
            }
            return arr;
        }
        return [];
    }

    private static CustomSetProperty(thisObj: object, data: object, property: string): void { }
}